qx.Class.define("caraffa.widgets.CirugiasBuscar",
{
	extend : qx.ui.window.Window,
	events:
	{
    "cirugiaFinded" : "qx.event.type.Data"
	},
    construct : function (titulo, fecha, turno){
    	this.fechaCir = fecha;
    	this._turno = turno;
		this.base(arguments);

		// RPC
		this.rpcCirugia = new qx.io.remote.Rpc();
		this.rpcCirugia.setTimeout(10000);
		this.rpcCirugia.setUrl("services/");
		this.rpcCirugia.setServiceName("cirugias");		
		try{
			var cargaForm = this.rpcCirugia.callSync("cargaFormBuscar");
			this.resPacientes = cargaForm.resPacientes;
		}
		catch(ex){
			alert("Error: " + ex);
		}
		// FORM
			this.setLayout(new qx.ui.layout.VBox(10));
			this.setCaption(titulo);
			this.setShowStatusbar(false);
			//this.setModal(false);
			this.setMovable(true);
			this.setResizable(false);
			this.setShowMaximize(false);
			this.setShowMinimize(false);
			this.moveTo(520,200);
			
			this.fechaD = new qx.ui.form.ow.DateText("Desde:");
			this.fechaH = new qx.ui.form.ow.DateText("Hasta:");
		this.mdlCirugiasB  = new qx.ui.table.model.Simple();
			this.mdlCirugiasB.setColumns(["Fecha","Cantidad"]);
		this.tblCirugiasB = new qx.ui.table.Table(this.mdlCirugiasB);
			this.tblCirugiasB.setStatusBarVisible(false);
			this.tblCirugiasB.setColumnVisibilityButtonVisible(false);
			this.tblCirugiasB.setColumnWidth(0, 100);
			this.tblCirugiasB.setColumnWidth(1, 200);
			this.tblCirugiasB.setShowCellFocusIndicator(false);
			this.tblCirugiasB.setWidth(120);
			this.tblCirugiasB.setHeight(200);			
			this.cmsPaciente = new qx.ui.form.ow.ComboFind("Paciente",this.resPacientes);
			
			this.botonera = new qx.ui.groupbox.GroupBox();
				this.botonera.setLayout(new qx.ui.layout.HBox(30).set({alignX:"center"}));
					this.btnAceptar = new qx.ui.form.Button("Buscar");
					this.btnMostrar = new qx.ui.form.Button("Mostrar");
					
				this.botonera.add(this.btnAceptar);
				this.botonera.add(this.btnMostrar);
							
			this.add(this.fechaD);				
			this.add(this.fechaH);
			this.add(this.cmsPaciente);
			this.add(this.tblCirugiasB);
			this.add(this.botonera);
			

			// EVENTS
			this.btnMostrar.addListener('execute', this._mostrar, this);
			this.btnAceptar.addListener('execute', this._Aceptar, this);
	},
    members :{
    	
		//RPC
		_Aceptar: function(){
				try{
					var params = new Object();
					params.fechaD = this.fechaD.getValueToSQL();
					params.fechaH = this.fechaH.getValueToSQL();
					params.dni_paciente = this.cmsPaciente.getValue();
					this.resCirugiasB = this.rpcCirugia.callSync("buscarCirugias", params );
					if(this.resCirugiasB.values != null){
						this.tblCirugiasB.getTableModel().setData(this.resCirugiasB.values);
					}
					else{
						alert("No se encontró ninguna cirugia");
					}
				}
				catch(ex){
					alert("Error: " + ex);
				}
		},
		_mostrar: function(){
			this.fechaCir.setValue(this.mdlCirugiasB.getValue(0, this.tblCirugiasB.getFocusedRow()));
			this.fireDataEvent('cirugiaFinded');
			this.close();
		}		
    }
});
